package com.cooper.scraper;

import java.util.List;

import com.cooper.android.comm.Resource;

public abstract class ResourceCollector<T extends Resource> {

	protected int pageCount = -1;
	protected List<T> medias;
	protected int pageNumber = 1;

	public ResourceCollector() {
		super();
	}

	public ResourceCollector(int pageNumber) {
		super();
		this.pageNumber = pageNumber;
	}

	public boolean BOF() {
		return medias == null;
	}

	public boolean EOF() {
		return (pageCount < pageNumber);
	}

	public boolean hasNextPage() {
		return (pageCount > pageNumber);
	}

	public void nextPage() {
		pageNumber++;
	}

	public int getPageCount() {
		return pageCount;
	}

	protected void setPageCount(int pageCount) {
		if (pageNumber <= pageCount || this.pageCount == -1) {
			this.pageCount = pageCount;
		}
	}

	public List<T> getMedias() {
		return medias;
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public void reset() {
		medias = null;
		pageNumber = 1;
		pageCount = -1;
	}

	public boolean noResult() {
		if (medias == null){
			throw new IllegalStateException("not call download yet");
		}else{
			return medias.isEmpty() && !hasNextPage();
		}	
	}

	public abstract String generateKey();

	public abstract ResultUrl scraperListUrl(ResourceScraper<T> scraper)
			throws ScraperException, DownloadException;

	public abstract void scraperResult(ResourceScraper<T> scraper, String page)
			throws ScraperException, DownloadException;

	public abstract void scraperResult(ResourceScraper<T> scraper, String page,
			String redirectUrl) throws ScraperException, DownloadException;

	public abstract boolean subscribed(String key);

}
